﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Drawing;
using XBLib.Dal.AdoDal;


public partial class JOINT_frmJ_StaffContractTypeInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("雇用情報登録");
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_StaffContractTypeInput");

		//Session["IdEmp"] = 164;
		//Session["ModeUpdateStaffEnterInput"] = "Update";

		if (Session["IdEmp"] == null)
		{
			Response.Redirect("../frmD_Login.aspx");
		}
		else if (Session["IdEmp"].ToString() == "")
		{
			Response.Redirect("../frmD_Login.aspx");
		}

        if (!IsPostBack)
        {
            if (Session["ModeUpdateStaffEnterInput"] != null)
            {
                if (Session["ModeUpdateStaffEnterInput"].Equals("Update"))
                {
                    btn_次.Visible = false;
                    btn_登録.Text = "更新";
                }
            }
            
            bind雇用区分();

            DateTime thisDay = DateTime.Now; 
            string year = thisDay.Year.ToString();
            string month = thisDay.Month < 10 ? "0" + thisDay.Month.ToString() : thisDay.Month.ToString();
            string date = thisDay.Day.ToString();

            txt実際開始日.Text = year + "/" + month + "/" + date;
            txt発令開始日.Text = year + "/" + month + "/" + date;

            ResetLoad雇用情報();

            string ER00001 = GetError("ER00001"); //Hãy nhập thông tin này
            string ER00005 = GetError("ER00005"); //Giá trị so sánh của ngày tháng không hợp lệ
            string ER00006 = GetError("ER00006"); //Định dạng ngày tháng không đúng
            string ER00007 = GetError("ER00007"); //Hãy chọn thông tin
            RequiredFieldValidator雇用区分.ErrorMessage = ER00007;
            CustomValidator発令開始日.ErrorMessage = ER00005;
            CustomValidator発令開始日1.ErrorMessage = ER00006;
            RequiredFieldValidator発令開始日.ErrorMessage = ER00001;
            CustomValidator発令完了日.ErrorMessage = ER00005;
            CustomValidator発令完了日1.ErrorMessage = ER00006;
            CustomValidator実際開始日.ErrorMessage = ER00005;
            CustomValidator実際開始日1.ErrorMessage = ER00006;
            CustomValidator実際開始日2.ErrorMessage = ER00005;
            RequiredFieldValidator実際開始日.ErrorMessage = ER00001;
            CustomValidator実際完了日.ErrorMessage = ER00005;
            CustomValidator実際完了日1.ErrorMessage = ER00006;
            CustomValidator実際完了日2.ErrorMessage = ER00005;
        }
        ddl雇用区分.Focus();
    }

    /// <summary>
    /// load du lieu len form khi o mode Update
    /// </summary>
    protected void ResetLoad雇用情報() 
	{
		if (Session["ModeUpdateStaffEnterInput"] != null && Session["ModeUpdateStaffEnterInput"].Equals("Update"))
		{
			int 雇用情報ID = Int32.Parse(Session["IdEmp"].ToString());
			雇用情報Manager frm = new 雇用情報Manager();
			DataTable dt = frm.Select雇用情報_ByID(雇用情報ID);
			if (dt.Rows.Count > 0)
			{
				ddl雇用区分.SelectedValue = dt.Rows[0]["雇用区分"].ToString() == "0" ? "00" : dt.Rows[0]["雇用区分"].ToString();
				txt発令開始日.Text = DateTime.Parse(dt.Rows[0]["発令開始日"].ToString()).ToString("yyyy/MM/dd");
				txt発令完了日.Text = dt.Rows[0]["発令終了日"].ToString() != "" ? DateTime.Parse(dt.Rows[0]["発令終了日"].ToString()).ToString("yyyy/MM/dd") : "";
				txt実際開始日.Text = DateTime.Parse(dt.Rows[0]["実際開始日"].ToString()).ToString("yyyy/MM/dd");
				txt実際完了日.Text = dt.Rows[0]["実際終了日"].ToString() != "" ? DateTime.Parse(dt.Rows[0]["実際終了日"].ToString()).ToString("yyyy/MM/dd") : "";
				txt変更理由.Text = dt.Rows[0]["変更理由"].ToString();
			}
		}
    }
    /// <summary>
    /// load du lieu vao dropdownList 雇用区分
    /// </summary>
    protected void bind雇用区分()
    {
        try
        {
            雇用情報Manager frm = new 雇用情報Manager();
            DataTable dt = frm.Select雇用区分();
            ddl雇用区分.DataSource = dt;
            ddl雇用区分.DataTextField = "雇用区分名";
            ddl雇用区分.DataValueField = "雇用区分";
            ddl雇用区分.DataBind();

            ListItem 選択 = new ListItem("[選択]", "-1");
            ddl雇用区分.Items.Insert(0, 選択);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    /// <summary>
    /// chuyen doi ki tu < >
    /// </summary>
    /// <param name="root"></param>
    /// <returns></returns>
    public static Control[] FlattenHierachy(Control root)
    {
        List<Control> list = new List<Control>();
        list.Add(root);
        if (root.HasControls())
        {
            foreach (Control control in root.Controls)
            {
                list.AddRange(FlattenHierachy(control));
            }
        }
        return list.ToArray();
    }
    protected void SetValueForControl()
    {
        Control[] allControls = FlattenHierachy(Page);
        foreach (Control control in allControls)
        {
            TextBox textBox = control as TextBox;
            if (textBox != null)
            {
                textBox.Text = textBox.Text.Replace("<", "&lt;");
                textBox.Text = textBox.Text.Replace(">", "&gt;");
            }
        }
    }
    protected void btn_登録_Click(object sender, EventArgs e)
    {
        SetValueForControl();

        雇用情報 obj = new 雇用情報();
        try
        {
            obj.社員ID = Int32.Parse(Session["IdEmp"].ToString());
            obj.廃止区分 = 0;
			obj.雇用区分_s = ddl雇用区分.SelectedValue;
            obj.雇用区分名 = ddl雇用区分.SelectedItem.ToString();
            obj.発令開始日 = DateTime.Parse(txt発令開始日.Text);
            if (txt発令完了日.Text != "")
            {
                obj.発令終了日 = DateTime.Parse(txt発令完了日.Text);
            }
            obj.実際開始日 = DateTime.Parse(txt実際開始日.Text);
            if (txt実際完了日.Text != "")
            {
                obj.実際終了日 = DateTime.Parse(txt実際完了日.Text);
            }
            obj.変更理由 = txt変更理由.Text;
        }
        catch (Exception ex)
        {
            return;
        }


        雇用情報Manager frm = new 雇用情報Manager();

		if (Session["ModeUpdateStaffEnterInput"] != null)
        {
			if (Session["ModeUpdateStaffEnterInput"].Equals("Insert"))
            {
                frm.Insert雇用情報(obj);
				
                Response.Redirect("frmJ_StaffDegreeByCompInput.aspx");
            }

            if (Session["ModeUpdateStaffEnterInput"].Equals("Update"))
            {
                int 雇用情報ID = Int32.Parse(Session["IdEmp"].ToString());
                frm.Update雇用情報(obj, 雇用情報ID);

                Response.Redirect("frmJ_EmpBasicInfo.aspx");
            }
        }
    }

    /// <summary>
    /// lay loi tu database
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
            error = dt.Rows[0][2].ToString();
        return error;
    }
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        if (Session["ModeUpdateStaffEnterInput"] != null){
            ResetLoad雇用情報();
        }
        else {
            Response.Redirect("frmJ_StaffContractTypeInput.aspx"); 
        }
    }
    protected void btn_戻る_Click(object sender, EventArgs e)
    {
		if (Session["ModeUpdateStaffEnterInput"] != null)
        {
			if (Session["ModeUpdateStaffEnterInput"].Equals("Insert"))
            {
				Response.Redirect("frmJ_StaffEnterInput.aspx");
            }
            if (Session["ModeUpdateStaffEnterInput"].Equals("Update"))
            {
                Response.Redirect("frmJ_EmpBasicInfo.aspx");
            }
        }
    }
    protected void btn_次_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_StaffDegreeByCompInput.aspx");
    }
}
